<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="2-20个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="2-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployees } from '@/api/employees '
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      // console.log(value)
      let codeList = this.originList.map(({ code }) => code)
      // console.log(codeList)
      // if (codeList.includes(value)) {
      //   callback(new Error(value + '已存在'))
      // } else {
      //   callback
      // }
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(({ code }) => code)
      }

      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validName = (rule, value, callback) => {
      // let nameList = this.originList.map(({ name }) => name)
      // if (codeList.includes(value)) {
      //   callback(new Error(value + '已存在'))
      // } else {
      //   callback
      // }
      let nameList = null

      nameList = this.originList.filter(item => item.pid === this.id).map(({ name }) => name)
      // console.log(nameList)
      if (this.isEdit) {
        // console.log(this.id)
        // 根据点击当前的id找到这个部门的pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // console.log(pid)
        // 查找兄弟部门，filter筛选数组中每个对象的pid和这个pid相等的并且对象的id和这个id不相等，然后再循环这个数组获得每个对象的name值      (({ name }) => name)  对对象里面的属性进行结构
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => name)
        console.log(nameList)
      }

      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入 2 ~ 10 个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入 2 ~ 10 个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 300, message: '请输入 2 ~ 300 个字符', trigger: 'blur' }
        ]
      },

      employees: []
    }
  },
  created() {
    this.loadEmployee()
    this.loadDetail()
  },
  methods: {
    async doAdd() {
      try {
        const params = { ...this.form, pid: this.id }
        // console.log(params)
        const res = await addDepartments(params)
        // console.log(res)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async doEdit() {
      try {
        const res = await updateDepartments(this.form)
        // console.log(res)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hCancel() {
      this.$emit('close')
    },
    async loadEmployee() {
      try {
        const res = await getEmployees()
        // console.log(res)
        this.employees = res.data
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hSubmit() {
      // this.doAdd()
      this.$refs.deptForm.validate(valid => {
        if (valid) this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async loadDetail() {
      if (!this.isEdit) return
      // console.log(this.id)
      const res = await getDepartDetail(this.id)
      // console.log(res)
      this.form = res.data
    }

  }
}
</script>
